package org.linloong.config.web;


import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dbo
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "sa-token")
public class WebConfig {

    private List<String> excludePath = new ArrayList<>();


    /**
     * 静态内部类：专门用来注册 Sa-Token 拦截器
     * 用 @Bean 方式注入，保证先读取完配置再注册
     */
    @Configuration
    public static class SaTokenWebMvcConfig implements WebMvcConfigurer {

        /* 注入上面那个配置类 */
        private final WebConfig props;

        public SaTokenWebMvcConfig(WebConfig props) {
            this.props = props;
        }

        // 添加sa-token拦截器
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
//            registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
//                    .addPathPatterns("/**")
//                    // 把配置文件里的排除地址一次性扔进去
//                    .excludePathPatterns(props.getExcludePath().toArray(new String[0]));
        }

        // 添加静态资源映射
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("doc.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }


}

